
import {Component} from 'react';

import emitter from '../event-emitter' ;

export default class SettleAccount extends Component {
    constructor(props){
        super();
        this.state = {
            total: props.total 
        }
        this.sum = props.sum ;
    }
    componentDidMount(){
        const name = '算账' ;
        const handler = () => {
            console.log( '谁让我算账我揍死谁'  );
            this.setState( {
                total: this.sum()
            });
        }
        // 自定义事件
        this.eventEmitter = emitter.addListener( name , handler );
    }
    componentWillUnmount(){
        // 移除自定义事件
        emitter.removeListener( this.eventEmitter );
    }
    render(){
        return (
            <div className='settle'>
                <span className='total'>{ this.state.total }</span>
                <span className='btn'>立即结算</span>
                <span className='clear'>清空购物车</span>
            </div>
        )
    }
}